package com.wjc.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import android.util.Log;

public class Dispatcher
{
   
    public Dispatcher(){}
    
    /**
     * 
     * @param host
     * @param port
     */
   
    
    /**
     * 
     * @param request - void
     * @author: 
     */
    public static boolean dispatch(HttpRequest request,Socket target)  throws IOException
    {
    	 target.setKeepAlive(true);
         
         OutputStream socketOutputStream = target.getOutputStream();
         InputStream socketInputStream   = target.getInputStream();
         
         request(request, socketOutputStream);
         
         HttpResponse httpResponse = HttpResponseFactory.read(socketInputStream, socketOutputStream);    
         resonse(httpResponse, request.getOutputStream());
         return httpResponse.isClosed();
     
    }
    
    /**
     * 
     * @param request
     * @throws IOException
     * @author: 
     */
   public static void request(HttpRequest request, OutputStream outputStream) throws IOException
    {
        /* 对请求头进行修改 */
        //updateHttpRequest(request,request.getRemoteHost(),request.getRemotePort());
        
        Log.d("请求"+Thread.currentThread().getId(),"------------------ 修改后的请求头 ------------------\r\n" + request.getHeaderBody());
        /* 写请求头 */
        if( request.getHost()!= null && !"".equals(request.getHost())) {
            outputStream.write( request.getHeaderBody().replaceFirst("http://" + request.getHost().replaceAll(".", "\\."), "").getBytes() );
            
        } else {
        	outputStream.write( request.getHeaderBody().getBytes() );
        }
   
        /* 将输入数据传输到服务器 */
        ContentPump.pumpContentStream(request.getInputStream(), outputStream, request.getContentLength());
       
        
    }
    
    /**
     * 
     * @param response
     * @throws IOException - void
     * @author: 
     */
    public static void resonse(HttpResponse response, OutputStream outputStream) throws IOException
    {
        /* 修改响应头 */
      //  updateHttpResponse(response);
        
        Log.d("响应"+Thread.currentThread().getId(), "------------------ 修改后的响应头 -----------------\r\n" + response.getHeaderBody());
        
        //outputStream = new PoolOutputStream(new OutputStream[]{outputStream, System.out});
        
        /* 写响应头 */
        outputStream.write(response.getHeaderBody().getBytes());
        
        /* 是否使用Chunk */
        if(response.getHeader("Transfer-Encoding") != null)
        {
            InputStream inputStream = response.getInputStream();
            
            ChunkedPump.pumpChunkedStream(inputStream, outputStream);
        }
        else if ( response.getContentLength() > 0 )
        {
        	ContentPump.pumpContentStream(response.getInputStream(), outputStream,  response.getContentLength());
        	
            
            //ContentPump.pumpContentStream(inputStream, outputStream, response.getContentLength());
        } else {
        	//Log.w(Dispatcher.class.getName(), "resonse连接出错 :" + response.getHeaderBody() );
        	//Log.d(Dispatcher.class.getName(), response.getContentLength() + ";" + response.getHeaderBody());
        	
        	byte[] byteArray1 = new byte[5120];
			int byteArray1Length = 0;

			while ((byteArray1Length = response.getInputStream()
					.read(byteArray1)) != -1) {
				outputStream.write(byteArray1, 0,
						byteArray1Length);
				outputStream.flush();
			}
        }
        outputStream.flush();
       
    }
    

    
    /**
     * 
     * @param request - void
     * @author: 
     */
    private static void updateHttpRequest(HttpRequest request,String host,int port)
    {
     
            request.setHeader("Connection", "Close");
   
        
  
    }
    
    /**
     * 
     * @param response - void
     * @author: 
     */
    private static void updateHttpResponse(HttpResponse response)
    {
        String header = response.getHeader("Connection");
        
        if(header != null)
        {
            Log.d("connection",response.getHeader("Connection"));
            response.setHeader("Connection", "Close");
           // if()
        }
    }
 

 
 
}
